package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmalDishMapper;
import com.sky.mapper.SetmalMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmalService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/*
 * 套餐管理的service
 * service层的作用：
 *   调用mapper层的方法查询数据库，如果需要将查询到的数据进行逻辑处理就在service层处理
 *   返回结果给Controller层
 * */
@Slf4j
@Service
public class SetmalServiceImp implements SetmalService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmalMapper setmalMapper;
    @Autowired
    SetmalDishMapper setmalDishMapper;

    /**
     * 查询所有套餐
     *
     * @param setmealPageQueryDTO
     * @return pageResult
     * @info 接收Controller层传递过来的分页参数，Service层再去调用Mapper层的方法去数据库中查询所有分类  Service->Mapper
     */
    @Override
    public PageResult fetchSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = (Page<SetmealVO>) setmalMapper.selectSetmealList(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param Ids
     * @return List<Setmeal>
     * @info 接收Controller层传递过来的id，Service层再去调用Mapper层的方法去数据库中删除所属分类  Service->Mapper
     */
    @Override
    public void deleteSetmealList(String ids) {
        List<Setmeal> setmealList = setmalMapper.selectSetmealListByIds(ids);
        for (Setmeal setmeal : setmealList) if (setmeal.getStatus() == 1) throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        setmalMapper.deleteSetmealList(ids);
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmalMapper.startOrStopSetmal(setmeal);
    }

    /**
     * 根据id查询套餐和套餐菜品关系
     *
     * @param id
     * @return
     */
    public SetmealVO fetchSetmalById(Long id) {
        SetmealVO setmealVO = new SetmealVO();

        Setmeal setmeal = setmalMapper.selectSetmealById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishList = setmalDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }
}
